
package com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;

import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.BaseSwipedAdapter;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.BaseSwipedViewHolder;


/**
 * swiped itemHelper的callback
 *
 * @author tys
 */
public class ItemTouchHelperCallback extends ItemTouchHelperExtension.Callback {

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(
                ItemTouchHelper.UP | ItemTouchHelper.DOWN,
                ItemTouchHelper.START | ItemTouchHelper.LEFT);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter instanceof BaseSwipedAdapter) {
            ((BaseSwipedAdapter) adapter).onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        }
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {}


    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (dY != 0 && dX == 0) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
        if (viewHolder instanceof BaseSwipedViewHolder) {
            BaseSwipedViewHolder vh = (BaseSwipedViewHolder) viewHolder;
            if (dX < -vh.getActionViewContainer().getWidth()) {
                //最多滑出菜单这么宽
                dX = -vh.getActionViewContainer().getWidth();
            }
            vh.getContentViewContainer().setTranslationX(dX);
        }
    }

}
